//
//  FloatSelectView.m
//  AifudaoDrawer
//
//  Created by Ji Hu on 12-6-5.
//  Copyright (c) 2012年 爱辅导. All rights reserved.
//

#import "FloatSelectView.h"

#define MODE_NONE 0
#define MODE_MOVE 1
#define MODE_RESIZE_LT 2
#define MODE_RESIZE_LB 3
#define MODE_RESIZE_RT 4
#define MODE_RESIZE_RB 5

@implementation FloatSelectView

@synthesize delegate;
@synthesize okButton;
@synthesize cancelButton;

-(void)checkSelectArea{
    
    //    NSLog(@"x:%f y:%f with:%f height:%f",mSelectArea.origin.x,mSelectArea.origin.y,mSelectArea.size.width,mSelectArea.size.height);
    
    BOOL needSetButtonFram=NO;
    
    if (mSelectArea.size.width<0) {
        mSelectArea.origin.x+=mSelectArea.size.width;
        mSelectArea.size.width=-mSelectArea.size.width;
        needSetButtonFram=YES;
    }
    
    if (mSelectArea.size.height<0) {
        mSelectArea.origin.y+=mSelectArea.size.height;
        mSelectArea.size.height=-mSelectArea.size.height;
        needSetButtonFram=YES;
    }
    
    if (mSelectArea.origin.x<0) {
        mSelectArea.size.width+=mSelectArea.origin.x;
        mSelectArea.origin.x=0;
        needSetButtonFram=YES;
    }
    
    if (mSelectArea.origin.y<0) {
        mSelectArea.size.height+=mSelectArea.origin.y;
        mSelectArea.origin.y=0;
        needSetButtonFram=YES;
    }
    
    CGFloat rOff=self.bounds.size.width-(mSelectArea.origin.x+mSelectArea.size.width);
    if (rOff<0) {
        mSelectArea.size.width+=rOff;
        needSetButtonFram=YES;
    }
    
    CGFloat bOff=self.bounds.size.height-(mSelectArea.origin.y+mSelectArea.size.height);
    if (bOff<0) {
        mSelectArea.size.height+=bOff;
        needSetButtonFram=YES;
    }
    
    if (needSetButtonFram) {
        [self.okButton setFrame:[self getOkButtonFrame]];
        [self.cancelButton setFrame:[self getCancelButtonFrame]];
    }
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        mode=MODE_NONE;
        cornerRectWidth=6;
           }
    return self;
}

-(void)didMoveToSuperview{
    
    CGFloat initWidth=200;
    CGFloat initHeight=130;
    mSelectArea = CGRectMake([self superview].frame.size.width/2-initWidth, [self superview].frame.size.height/2-initHeight, initWidth*2, initHeight*2);
    [self setOpaque:NO];
//    [self setAlpha:0.5];
    
    self.okButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.okButton setFrame:[self getOkButtonFrame]];
    [self.okButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.okButton setOpaque:YES];
    [self.okButton addTarget:self action:@selector(okClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.okButton];
    [self bringSubviewToFront:self.okButton]; 
    
    self.cancelButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.cancelButton setFrame:[self getCancelButtonFrame]];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setOpaque:YES];
    [self.cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.cancelButton];
    [self bringSubviewToFront:self.cancelButton]; 
}

-( CGRect )getOkButtonFrame{
    return CGRectMake(mSelectArea.origin.x+mSelectArea.size.width/2-60, mSelectArea.origin.y, 60, 40);
}

-( CGRect )getCancelButtonFrame{
    return CGRectMake(mSelectArea.origin.x+mSelectArea.size.width/2, mSelectArea.origin.y, 60, 40);
}

-(void)okClick{
    [self.delegate onSelectComplete:mSelectArea];
}

-(void)cancelClick{
    [self.delegate onCancel];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    CGPoint begin=[[touches anyObject] locationInView:self];
    
    CGFloat tw=cornerRectWidth+15;
    
    if(CGRectContainsPoint([self getCornerRect:MODE_RESIZE_LT withWidth:tw], begin)){
        mode=MODE_RESIZE_LT;
    }else if(CGRectContainsPoint([self getCornerRect:MODE_RESIZE_LB withWidth:tw], begin)){
        mode=MODE_RESIZE_LB;
    }else if(CGRectContainsPoint([self getCornerRect:MODE_RESIZE_RT withWidth:tw], begin)){
        mode=MODE_RESIZE_RT;
    }else if(CGRectContainsPoint([self getCornerRect:MODE_RESIZE_RB withWidth:tw], begin)){
        mode=MODE_RESIZE_RB;
    }else if (CGRectContainsPoint(mSelectArea, begin)) {
        mode=MODE_MOVE;
    }else {
        mode=MODE_NONE;
    }
    
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    CGPoint move=[[touches anyObject] locationInView:self];
    CGPoint last=[[touches anyObject] previousLocationInView:self];
    
    CGFloat dx=move.x-last.x;
    CGFloat dy=move.y-last.y;
    
    if (mode==MODE_MOVE) {
         mSelectArea=CGRectOffset(mSelectArea, dx, dy);
    }else if (mode==MODE_RESIZE_LT) {
        mSelectArea.origin.x+=dx;
        mSelectArea.origin.y+=dy;
        
        mSelectArea.size.width-=dx;
        mSelectArea.size.height-=dy;
    }else if (mode==MODE_RESIZE_LB) {
        mSelectArea.origin.x+=dx;
//        mSelectArea.origin.y+=dy;
        
        mSelectArea.size.width-=dx;
        mSelectArea.size.height+=dy;
    }else if (mode==MODE_RESIZE_RT) {
//        mSelectArea.origin.x+=dx;
        mSelectArea.origin.y+=dy;
        
        mSelectArea.size.width+=dx;
        mSelectArea.size.height-=dy;
    }else if (mode==MODE_RESIZE_RB) {
        
        mSelectArea.size.width+=dx;
        mSelectArea.size.height+=dy;
    }
    
    
    [self.okButton setFrame:[self getOkButtonFrame]];
    [self.cancelButton setFrame:[self getCancelButtonFrame]];
    
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self checkSelectArea];
    [self setNeedsDisplay];
    
}



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextStrokeRect(context, [self getCornerRect:MODE_RESIZE_LT withWidth:cornerRectWidth]);
    CGContextStrokeRect(context, [self getCornerRect:MODE_RESIZE_LB withWidth:cornerRectWidth]);
    CGContextStrokeRect(context, [self getCornerRect:MODE_RESIZE_RB withWidth:cornerRectWidth]);
    CGContextStrokeRect(context, [self getCornerRect:MODE_RESIZE_RT withWidth:cornerRectWidth]);
    
    float lengths[] = {10,10};  
    CGContextSetLineDash(context, 0, lengths,2);  
    CGContextStrokeRectWithWidth(context, mSelectArea, 1);
    
    
}

-(CGRect) getCornerRect:(int)position withWidth:(CGFloat)width{
    
    CGFloat templenth=width*2;
    
    if (position==MODE_RESIZE_LT) {
        return CGRectMake(mSelectArea.origin.x-width, mSelectArea.origin.y-width, templenth, templenth);
    }else if (position==MODE_RESIZE_LB) {
        return CGRectMake(mSelectArea.origin.x-width, mSelectArea.origin.y-width+mSelectArea.size.height, templenth, templenth);
    }else if(position==MODE_RESIZE_RT){
        return CGRectMake(mSelectArea.origin.x-width+mSelectArea.size.width, mSelectArea.origin.y-width, templenth, templenth);
    }else {
        return  CGRectMake(mSelectArea.origin.x-width+mSelectArea.size.width, mSelectArea.origin.y-width+mSelectArea.size.height, templenth, templenth);
    }
    
}

-(void) dealloc{
    [self.okButton release];
    [self.cancelButton release];
    [super dealloc];
}

@end
